package com.yhy.dubbo.service.mongo;

import com.yhy.domain.mongo.UserLocation;
import com.yhy.dubbo.api.mongo.UserLocationApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metric;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Double latitude, Double longitude, String address, Long userId) {
        //判断当前userId用户是否有位置信息,如果没有则创建 如果有则更新
        Query query = new Query(Criteria.where("userId").is(userId));

        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if(location == null){
            location = new UserLocation();
            location.setId(ObjectId.get());
            location.setUserId(userId);
            location.setAddress(address);
            GeoJsonPoint point = new GeoJsonPoint(longitude,latitude);
            location.setLocation(point);
            location.setCreated(System.currentTimeMillis());
            location.setUpdated(System.currentTimeMillis());
            location.setLastUpdated(System.currentTimeMillis());
            mongoTemplate.save(location);
        }else {
            Update update = new Update();
            update.set("address",address);
            GeoJsonPoint point = new GeoJsonPoint(longitude,latitude);
            update.set("location",point);
            update.set("updated",System.currentTimeMillis());
            update.set("lastUpdated",location.getUpdated());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }
    }

    @Override
    public List<Long> searchNear(Long userId, String distance) {
        //1.根据Id查询当前位置信息
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation location = mongoTemplate.findOne(new Query(criteria), UserLocation.class);
        GeoJsonPoint point = location.getLocation();
        //2.构建查找半径
        Long mile = Long.valueOf(distance);
        Metric metric;
        Distance distance1 = new Distance(mile/1000, Metrics.KILOMETERS);
        //3.根据位置信息和半径画圆
        Circle circle = new Circle(point,distance1);
        //4.构造查询条件
        Criteria locationCriteria = Criteria.where("location").withinSphere(circle);
        //5.查询出所有满足条件的UserLocation
        List<UserLocation> locationList = mongoTemplate.find(new Query(locationCriteria), UserLocation.class);
        //6.封装userlocation里的id
        List<Long> userIds = new ArrayList<>();
        for (UserLocation userLocation : locationList) {
            userIds.add(userLocation.getUserId());
        }
        //7.构造返回
        return userIds;
    }
}
